<?php

namespace app\common\entity;

use think\Db;
use think\Model;
use app\common\entity\User;

class GoodsOrders extends Model
{
    /**
     * @var string 对应的数据表名
     */
    protected $table = 'orders';
    const TYPE_SEND = 1;    //发货
    const TYPE_CANCEL = 2;  //取消发货
    //订单状态
    static $status = array(
        '0' => '待付款',
        '1' => '待发货',
        '2' => '待收货',
        '3' => '已完成',
        '4' => '订单取消'
    );

    public static function getAllCate()
    {
        return [
            ["name"=>"圆通速递","key"=>"yuantong","type"=>"国内运输商"],
            ["name"=>"韵达快递","key"=>"yunda","type"=>"国内运输商"],
            ["name"=>"中通快递","key"=>"zhongtong","type"=>"国内运输商"],
            ["name"=>"申通快递","key"=>"shentong","type"=>"国内运输商"],
            ["name"=>"百世快递","key"=>"huitongkuaidi","type"=>"国内运输商"],
            ["name"=>"顺丰速运","key"=>"shunfeng","type"=>"国际运输商"],
            ["name"=>"EMS","key"=>"ems","type"=>"国际邮政"],
        ];
    }
    public static function getOneCate($key)
    {

        $arr =  [
            ["name"=>"圆通速递","key"=>"yuantong","type"=>"国内运输商"],
            ["name"=>"韵达快递","key"=>"yunda","type"=>"国内运输商"],
            ["name"=>"中通快递","key"=>"zhongtong","type"=>"国内运输商"],
            ["name"=>"申通快递","key"=>"shentong","type"=>"国内运输商"],
            ["name"=>"百世快递","key"=>"huitongkuaidi","type"=>"国内运输商"],
            ["name"=>"顺丰速运","key"=>"shunfeng","type"=>"国际运输商"],
            ["name"=>"EMS","key"=>"ems","type"=>"国际邮政"],
        ];
        foreach($arr as $k=>$v){
            if($v['key'] == $key){
                return $v;
            }
        }
        return ["name"=>"","key"=>"","type"=>""];
    }

    /*
     * 分页订单列表
     * */
    public function getGoodsOrdersPages($search, $type, $status)
    {

        if ($search) {
            if ($type == 'mobile') {

                $pageParam['query']['search'] = $search;
                $pageParam['query']['type'] = $type;
                $query = new self();
                $query = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.goods_title,g.goods_pic');
                $query = $query->where('u.mobile', $search);
                if ($status) {

                    if ($status != -1) {
                        $query = $query->where('o.status', $status);
                    }
                }
                $data = $query->leftJoin("user u", 'u.id = o.user_id')
                    ->leftJoin('goods g', 'g.id = o.goods_id')
                    ->order('create_time desc')
                    ->paginate(15, false, $pageParam);
            } elseif ($type == 'number') {
                $map['order_number'] = $search;

                $pageParam['query']['search'] = $search;
                $pageParam['query']['type'] = $type;
                $query = new self();
                $data = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.goods_title,g.goods_pic')
                    ->leftJoin("user u", 'u.id = o.user_id')
                    ->leftJoin('goods g', 'g.id = o.goods_id')
                    ->where($map)
                    ->order('create_time desc')
                    ->paginate(15, false, $pageParam);
            }
        } else {
            $query = new self();
            $query = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.goods_title,g.goods_pic');
            if ($status) {

                if ($status != -1) {
                    $query = $query->where('o.status', $status);
                }
            }
            $data1 = $this->select();
            $count = count($data1);
            $data = $query->leftJoin("user u", 'u.id = o.user_id')->leftJoin('goods g', 'g.id = o.goods_id')->order('create_time desc')->paginate(15,false);

        }
        return $data;
    }

    /*
     *@param   user_id
     *  根据用户ID获取用户名
     * */
    public function getUserNameByUserId($user_id)
    {
        $userModel = new \app\common\entity\User();
        $userName = $userModel->getNameByUserId($user_id);
        return $userName;
    }

    /*
    *@param   user_id
    *  根据用户ID获取用户手机号
    * */
    public function getUserMobileByUserId($user_id)
    {
        $userModel = new \app\common\entity\User();
        $userName = $userModel->getMobileByUserId($user_id);
        return $userName;
    }

    /*
     * @param $key   status 的键
     * @return $statusName
     * */
    public function getStatusName($key)
    {
        $statusName = self::$status;
        return $statusName[$key];
    }

    /*
     * @param  $order_number
     * 根据订单号获取商品信息
     * */
    public function getGoods($order_number)
    {
        $goodsList = Db::field('o.*,c.*')
            ->table(['orders' => 'o', 'goods' => 'c'])
            ->where('o.goods_id=c.id and o.order_number=' . $order_number)
            ->select();

        $userModel = new \app\common\entity\Goods();
        $data = '';
        foreach ($goodsList as $k => $v) {

            $info = $userModel->getGoodsDetail($v['goods_id']);
            $pics = $userModel->getGoodsPic($v['goods_id']);
            $data .= '<p>' . $info["goods_title"] . '<img src="' . $pics[0] . '">' . '<div> 商品数量: ' . $v['goods_num'] . '</div>' . "</p>";
        }


        return $data;
    }

    /*
     * $type  发货或者取消发货的标识
     * */
    public function updateGoodsOrders(GoodsOrders $goodsOrders, $data, $type)
    {
        if ($type == 1) {
            $goodsOrders->express = $data['express'];
            $goodsOrders->expressnumber = $data['expressnumber'];
            $goodsOrders->status = 2;
            $goodsOrders->send_time = time();
        } else {
            $goodsOrders->status = 1;
        }

        return $goodsOrders->save();
    }

}